package cn.growthgate.fgo.web;

import java.util.Arrays;
import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;

import cn.growthgate.fgo.common.Constants;
import cn.growthgate.fgo.common.base.BaseVo;
import cn.growthgate.fgo.entity.Menu;
import cn.growthgate.fgo.service.IMenuService;
import cn.growthgate.fgo.util.CacheUtils;
import cn.growthgate.fgo.util.StringUtils;
import cn.growthgate.fgo.web.result.JsonResult;
import cn.growthgate.fgo.web.result.TableResult;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author GrowthGate
 * @since 2019-01-20
 */
@Controller
@RequestMapping("/menu")
@RequiresRoles(value = {Constants.ROLE_ADMIN}, logical = Logical.OR)
public class MenuController {

	@Autowired
	private IMenuService menuService;

	@RequestMapping("/list")
	public ModelAndView list() {
		List<Menu> rootMenus = menuService.getRootMenus();

		return new ModelAndView("menu/list").addObject("rootMenus", rootMenus);
	}

	@ResponseBody
	@RequestMapping("/tableData")
	public TableResult<BaseVo<Menu>> tableData(Menu menu, Integer current, Integer size, String orderByField,
			Boolean isAsc) {
		Wrapper<Menu> wrapper = new EntityWrapper<>();

		if (menu.getParentId() != null) {
			wrapper.eq("parentId", menu.getParentId());
		}
		if (menu.getType() != null) {
			wrapper.eq("type", menu.getType());
		}
		if (StringUtils.isNotBlank(menu.getName())) {
			wrapper.like("name", menu.getName());
		}
		if (StringUtils.isNotBlank(menu.geteName())) {
			wrapper.like("eName", menu.geteName());
		}

		Page<Menu> page = menuService.selectPage(current, size, orderByField, isAsc, wrapper);
		return new TableResult<BaseVo<Menu>>(menuService.transformVo(page));
	}

	@RequestMapping("/form")
	public ModelAndView form(Long id) {
		Menu entity = menuService.selectById(id);
		List<Menu> rootMenus = menuService.getRootMenus();

		return new ModelAndView("menu/form").addObject("entity", entity).addObject("rootMenus", rootMenus);
	}

	@ResponseBody
	@RequestMapping("/save")
	public JsonResult save(@Validated Menu menu, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return JsonResult.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
		}
		menuService.insertOrUpdate(menu);
		// 清除缓存
		CacheUtils.clear(Constants.CACHE_USER_MENUS);
		return JsonResult.ok();
	}

	@ResponseBody
	@RequestMapping("/delete")
	public JsonResult delete(Long[] ids) {
		if (menuService.deleteBatchIds(Arrays.asList(ids))) {
			// 清除缓存
			CacheUtils.clear(Constants.CACHE_USER_MENUS);
			return JsonResult.ok("删除成功");
		} else {
			return JsonResult.fail("删除失败");
		}
	}

}
